#include <bits/stdc++.h>
using namespace std;

char buf[(int)5e7], *fs = buf;

inline int gn() { 
    int k = 0;
    char c = *fs++;
    for (; !isdigit(c); c = *fs++);
    for (; isdigit(c); c = *fs++) k = k * 10 + c - '0';
    return k;
}

const int maxn = 1e5 + 3;

int n, p[maxn];

struct edge { 
    int fr, to, w;
    edge(int fr, int to, int w) : fr(fr), to(to), w(w) {}
    bool operator < (const edge &x) { 
        return w < x.w;
    }
};

vector<edge> e;

int fa[maxn];

int getf(int x) { 
    if (x == fa[x]) return x;
    else return fa[x] = getf(fa[fa[x]]);
}

void merge(int x, int y) { 
    int fx = getf(x), fy = getf(y);
    fa[fy] = fx;
}

int main() { 
    //freopen("autosadism.in", "r", stdin);
    //freopen("autosadism.out", "w", stdout);
    fread(buf, 1, (int)5e7, stdin);
    n = gn();
    for (int i = 1; i <= n; i++) 
        p[i] = gn();
    for (int i = 1; i <= n; i++) 
        for (int j = i + 1; j <= n; j++) 
            e.push_back(edge(i, j, min(p[i] % p[j], p[j] % p[i])));
    sort(e.begin(), e.end());
    int ans = 0;
    for (int i = 1; i <= n; i++) { 
        fa[i] = i;
    }
    for (int i = 0; i < e.size(); i++) { 
        if (getf(e[i].fr) != getf(e[i].to)) { 
            ans += e[i].w;
            merge(e[i].fr, e[i].to);
        }
    }
    cout << ans << '\n';
}
